<?php

session_start();
//wi6加密

function wi6($value)
{
  $s = bin2hex(gzcompress(base64_encode(convert_uuencode($value))));
  $s = str_replace('e', '&', $s);
  $s = str_replace('a', '#', $s);
  $s = str_replace('0', '$', $s);
  $s = str_replace('3', '.', $s);
  $s = str_replace('6', '!', $s);
  $s = str_replace('9', '(', $s);
  $s = str_replace('1', ')', $s);
  $s = str_replace('4', '=', $s);
  $s = str_replace('5', '+', $s);
  $s = str_replace('d', '~', $s);
  $s = str_replace('7', '@', $s);
  return md5(str_replace('2', '%', $s));
}
//获取IP
function getIP()
{
global $ip;
if (getenv("HTTP_CLIENT_IP"))
  $ip = getenv("HTTP_CLIENT_IP");
else if(getenv("HTTP_X_FORWARDED_FOR"))
  $ip = getenv("HTTP_X_FORWARDED_FOR");
else if(getenv("REMOTE_ADDR"))
  $ip = getenv("REMOTE_ADDR");
else $ip = "NULL";
return $ip;
}
//加密函数
function jupass($src)
{
	return md5(bin2hex(base64_encode(convert_uuencode($src))));
}
// 敏感词过滤，返回结果
function sensitive($list, $string){
  $count = 0; //违规词的个数
  $sensitiveWord = '';  //违规词
  $stringAfter = $string;  //替换后的内容
  @$pattern = "/".implode("|",$list)."/i"; //定义正则表达式
  if(preg_match_all($pattern, $string, $matches)){ //匹配到了结果
    $patternList = $matches[0];  //匹配到的数组
    $count = count($patternList);
    $sensitiveWord = implode(',', $patternList); //敏感词数组转字符串
    $replaceArray = array_combine($patternList,array_fill(0,count($patternList),'*')); //把匹配到的数组进行合并，替换使用
    $stringAfter = strtr($string, $replaceArray); //结果替换
  }
  return $stringAfter;
}
//添加表白
function add($n1,$n2,$string)
{
  //判断是否已经提交过
  if (isset($_COOKIE['ip'])) {die(json_encode(array('code' => 0, 'msg'=>'请五分钟后再提交！'),JSON_UNESCAPED_UNICODE));}
  $list = ['鸡巴', '煞笔', '傻逼', '逼', '操比', '王八蛋', '屎', '草泥马','操你妈','处女膜','奶子','乳房','咪咪','搞基','骚','尿','屁眼','尿道','阴道','狗日','日狗','阳具','杀人','小鸡鸡','撸管','自慰','胸罩','大姨妈','蛋子','我日','口交','丝袜','恶心','乳头','射精','祖宗','SB','sb','2b','手淫','大便','淫水','高潮','潮喷','龟头','抛尸','肢解','SM','吃人','辣鸡','神经病','鞭尸','强奸','sm','交配','艹','肏','jb','JB','干你妈','色情','淫秽'];
	//过滤脏字
	@$name1 = sensitive($list, $n1);
	@$name2 = sensitive($list, $n2);
  @$info = sensitive($list, $string);
  //判断是否为空
  if ($n1=='') {die(json_encode(array('code' => 0, 'msg'=>'请填写完整！'),JSON_UNESCAPED_UNICODE));}
  if ($n2=='') {die(json_encode(array('code' => 0, 'msg'=>'请填写完整！'),JSON_UNESCAPED_UNICODE));}
  if ($string=='') {die(json_encode(array('code' => 0, 'msg'=>'请填写完整！'),JSON_UNESCAPED_UNICODE));}
	//存放cookies
	setcookie("name1", $name1, time()+3600);
  setcookie("name2", $name2, time()+3600);
  setcookie("info", $info, time()+3600);
	$t = date('Y-m-d h:i:s');
  //存进Mysql
  $name = $name1.'&'.$name2;
	if (DB::query("INSERT INTO love_list (name, name1, name2, info, `time`, s) VALUES ('$name','$name1', '$name2', '$info', '$t', 0)")) {
		echo json_encode(array('code' => 1, 'msg'=>'提交成功！'),JSON_UNESCAPED_UNICODE);
    setcookie("ip", getIP(), time()+300);
	}else{
		echo json_encode(array('code' => 0, 'msg'=>'提交失败！'),JSON_UNESCAPED_UNICODE);
	}
}
//搜索关键字
function search($name)
{
	$result = DB::query("SELECT * FROM love_list WHERE name LIKE '%$name%'");
  		if (DB::affected($result) > 0) {
    	// 输出数据
   			while($row = DB::fetch($result)) {
   				$json['data'][] = $row;
    		}
        echo json_encode($json,JSON_UNESCAPED_UNICODE);
  	}
}
//所有
function see($page)
{
  $p = ($page-1)*8;
  $result = DB::query("SELECT * FROM love_list ORDER BY Id DESC limit $p, 6");
  if (DB::affected($result) > 0) {
    // 输出数据
    while($row = DB::fetch($result)) {
      $json['data'][] = $row;
    }
    echo json_encode($json,JSON_UNESCAPED_UNICODE);
  }
}
//点赞
function like($id)
{
  if (isset($_COOKIE['s'])) {die('
    <script src="assets/js/jquery.min.js"></script><script src="assets/layer/layer.js"></script><script>layui.use("layer", function(){layer.confirm("您已点赞，请五分钟后再来！", {btn: ["确定"],btn1:function(index,layero){window.location.href="./";}});});</script>');}
  $src = DB::get_row("SELECT * FROM love_list WHERE Id = '$id'");
  $s = $src['s']+1;
  $result = DB::query("UPDATE love_list SET s = '$s' WHERE Id = '$id'");
      if ($result) {
        setcookie("s", getIP(), time()+300);
        die('
    <script src="assets/js/jquery.min.js"></script><script src="assets/layer/layer.js"></script><script>layui.use("layer", function(){layer.confirm("点赞成功！", {btn: ["确定"],btn1:function(index,layero){window.location.href="./";}});});</script>');
    }
}
//页数
function s()
{
  echo DB::affected(DB::query("SELECT Id FROM love_list"));;
}
//修改后台配置
function set()
{
  if (!isset($_SESSION['love'])) {exit();}
//修改后台配置
  $user = $_POST['user'];$pwd = wi6($_POST['pwd']);$title = $_POST['title'];$seo = $_POST['seo'];$foot = $_POST['foot'];//$title = $_POST['title'];
  if ($_POST['pwd'] == '') {$x = DB::query("UPDATE love_config SET user = '$user',title = '$title',seo = '$seo',foot = '$foot' WHERE Id = 1");
  }else{$x = DB::query("UPDATE love_config SET user = '$user',pwd = '$pwd',title = '$title',seo = '$seo',foot = '$foot' WHERE Id = 1");}
  if ($x) {
    echo json_encode(array('code' => 1, 'msg'=>'修改成功！'),JSON_UNESCAPED_UNICODE);
  }else{
    echo json_encode(array('code' => 0, 'msg'=>'修改失败！'),JSON_UNESCAPED_UNICODE);
  }
}
//登陆
function login()
{
  $data = DB::get_row("SELECT user,pwd FROM love_config WHERE Id = 1");
  $user = $_POST['user'];$pwd = wi6($_POST['pwd']);
  if ($data['user'] == $user && $data['pwd'] == $pwd) {
    echo json_encode(array('code' => 1, 'msg'=>'登陆成功！'),JSON_UNESCAPED_UNICODE);
    $_SESSION['love'] = $user;
  }else{
    echo json_encode(array('code' => 0, 'msg'=>'登陆失败！'),JSON_UNESCAPED_UNICODE);
  }
}